package com.lrm.web.admin;

import com.lrm.po.User;
import com.lrm.service.UserService;
import com.lrm.util.AliOSSUtils;
import com.lrm.util.MD5Utils;
import org.apache.catalina.connector.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;


@Controller
//@RequestMapping("/admin")
public class LoginController {
    @Autowired
    private AliOSSUtils aliOSSUtils;

    @Autowired
    private UserService userService;

    @GetMapping("/admin/login")
    public String loginPage() {
        return "admin/login";
    }

    @PostMapping("/admin/login")//postmapping
    public String login(@RequestParam String username,
                        @RequestParam String password,
                        HttpSession session,
                        RedirectAttributes attributes) {

        User user = userService.checkUser(username,password);
        System.out.println(user);
        if(user != null){
            //验证成功，记录Session信息
            user.setPassword(null);
            session.setAttribute("user",user);
            return "/admin/index";
        }
        else {
          attributes.addFlashAttribute("message", "用户名和密码错误");
          return "redirect:/admin/login";
      }

    }

    @GetMapping("/admin/logout")//getmapping
    public String logout(HttpSession session) {
        session.removeAttribute("user");
        return "redirect:/admin/login";
    }

    /**
     * 注册页面
     * @return
     */
    @RequestMapping("/user/register")
    public String register(){
        return "user/register";
    }

    /**
     * 判断是否成功注册
     * @param request
     * @return
     */
    @RequestMapping("/doregister")
    public String register(HttpServletRequest request, RedirectAttributes attributes, @RequestParam("fileName")MultipartFile image) throws IOException {
        String name = request.getParameter("username");
        String password = request.getParameter("password");
        String password2 = request.getParameter("password2");
        String nickName = request.getParameter("nickname");
        List<User> userList = userService.findByName(name);
        System.out.println(userList);
        if(userList.size() == 0 && password2.equals(password)){
            User user = new User();
            System.out.println("新建了一个user，userId = "+user.getId());
            if(!image.isEmpty()){
                user.setAvatar(aliOSSUtils.upload(image));
            }
            user.setUsername(name);
            user.setNickname(nickName);
            user.setPassword(MD5Utils.code(password));
            userService.save(user);
            return "admin/login";
        }else{
            if(userList.size() != 0){
                attributes.addFlashAttribute("message", "用户名已存在");
            } else {
                attributes.addFlashAttribute("message", "两次输入的密码不匹配");
            }
            return "redirect:user/register";
        }
    }
}
